package cn.gov.msa.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Question;
import cn.gov.msa.service.QuestionService;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/question", produces = "text/plain;charset=UTF-8")
public class QuestionController {
	
	ObjectMapper mapper = new ObjectMapper();
	
	@Autowired
	private QuestionService questionService;
		
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params) throws Exception {	
		
		String result = "";		
		int pageNum = 1;
		int pageSize = 10;
		System.out.println(params);
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}	
		Question question = GsonHelper.convertEntity(params.toString(), Question.class);
		
		List<Question> questionList =questionService.selectData(pageNum, pageSize, question);	
	    //获取分页信息
        PageInfo<Question> page = new PageInfo<Question>(questionList);       
		JSONArray ja = JSONArray.fromObject(questionList);				
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}
	
	@RequestMapping(value = "/qlist")
	public String qlist(@RequestBody JSONObject params) throws Exception {	
		
		String result = "";		
		int pageNum = 1;
		int pageSize = 10;
		System.out.println(params);
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}	
		Question question = GsonHelper.convertEntity(params.toString(), Question.class);
		
		List<Question> questionList =questionService.selectDataq(pageNum, pageSize, question);	
	    //获取分页信息
        PageInfo<Question> page = new PageInfo<Question>(questionList);       
		JSONArray ja = JSONArray.fromObject(questionList);				
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}
		
	
	@RequestMapping(value = "/changeState")
	public String changeState(Question question) throws Exception {

		JSONObject jsonResult = new JSONObject();
		
		int result=questionService.updateState(question);
		
		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
	
	@RequestMapping(value = "/insert")
	public String insert(@RequestBody JSONObject param) throws Exception {
				

		JSONObject jsonResult = new JSONObject();
		
		System.err.println(param);
//		Question question =GsonHelper.convertEntity(param.toString(), Question.class);
		
//		int result = questionService.insertW(question);
		jsonResult = questionService.insert(param);
		
//		jsonResult.put("result", result);
//		jsonResult.put("pk",question.getQUESTIONNAIREID());
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception {	
				

		JSONObject jsonResult = new JSONObject();
//		
//		System.err.println(param);
//		Question question =GsonHelper.convertEntity(param.toString(), Question.class);
		
		jsonResult = questionService.updateData(param);
		
//		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/delete")
	public String delete(Question question) throws Exception {	
				
		JSONObject jsonResult = new JSONObject();
				
		int result=questionService.delete(question);
		questionService.deleteq(question);
		questionService.deleteItem(question);		
		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/insertq")
	public String insertq(@RequestBody JSONObject param) throws Exception {	
				

		JSONObject jsonResult = new JSONObject();
		
		System.err.println(param);
		Question question =GsonHelper.convertEntity(param.toString(), Question.class);
		
		int result=questionService.insertq(question);
		
		if(question.getQUESTIONITEM().indexOf(",")==-1) {
			question.setNUM("1");
			questionService.insertItem(question);						
		}else {
			String[] items=question.getQUESTIONITEM().split(",");
			for (int i = 0; i < items.length; i++) {
				question.setQUESTIONITEM(items[i]);
				question.setNUM((i+1)+"");
				questionService.insertItem(question);
			}			
		}
		jsonResult.put("info", "数据保存成功");		
		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/updateq")
	public String updateq(@RequestBody JSONObject param) throws Exception {	
				

		JSONObject jsonResult = new JSONObject();
		
		System.err.println(param);
		Question question =GsonHelper.convertEntity(param.toString(), Question.class);
		
		int result=questionService.updateq(question);
		questionService.deleteItem(question);
		if(question.getQUESTIONITEM().indexOf(",")==-1) {	
			question.setNUM("1");
			questionService.insertItem(question);						
		}else {
			String[] items=question.getQUESTIONITEM().split(",");
			for (int i = 0; i < items.length; i++) {
				question.setQUESTIONITEM(items[i]);
				question.setNUM((i+1)+"");
				questionService.insertItem(question);
			}			
		}
		jsonResult.put("info", "数据保存成功");		
		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/deleteq")
	public String deleteq(Question question) throws Exception {	
				
		JSONObject jsonResult = new JSONObject();
				
		int result=questionService.deleteq(question);
		questionService.deleteItem(question);		
		jsonResult.put("result", result);
		
		return jsonResult.toString();
	}
	
	
	
	
	/*@RequestMapping(value = "/insertRule")
	public String insetRule(@RequestBody JSONObject param) throws Exception {		
		int result = 0;
		String info = "";
	    boolean save = false; 		
	    //得到类型的model数据    
	    try{	
			Rule rule = GsonHelper.convertEntity(param.toString(), Rule.class);
			ruleService.insertRule(rule);
		    save = true;
	    }
	    catch(Exception e){
	    	result = 2;
	    	info = "数据保存异常！";
	    }	    	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    
    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();			
	}
	
	@RequestMapping(value = "/deleteRule")
	public String deleteRule(Rule rule) throws Exception {		
	
		 JSONObject jsonResult = new JSONObject();	
		 
		 ruleService.deleteRule(rule);	    
		 jsonResult.put("result", "1");

		return jsonResult.toString();			
	}
	
	
	@RequestMapping(value = "/batchDelete")
	public String batchDelete(@RequestParam("ruleIds")String ruleIds) throws Exception {		
	
		 JSONObject jsonResult = new JSONObject();	
		 
		 if(ruleIds.indexOf(",")==-1) {
			 Rule rule =new Rule();
			 rule.setRULEID(ruleIds);
			 ruleService.deleteRule(rule);	
		 }else {
			 String[] ruleId=ruleIds.split(",");
			 for (String string : ruleId) {
				 Rule rule =new Rule();
				 rule.setRULEID(string);
				 ruleService.deleteRule(rule);
			}
		 }
		 
		jsonResult.put("result", "1");
		return jsonResult.toString();			
	}
	
	@RequestMapping(value = "/updateRule")
	public String updateRule(@RequestBody JSONObject param) throws Exception {		
		int result = 0;
		String info = "";
	    boolean save = false; 		
	    //得到类型的model数据    
	    try{	
			Rule rule = GsonHelper.convertEntity(param.toString(), Rule.class);
			ruleService.updateRule(rule);
		    save = true;
	    }
	    catch(Exception e){
	    	result = 2;
	    	info = "数据保存异常！";
	    }	    	    
	    JSONObject jsonResult = new JSONObject();
	    if(save){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    
    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();			
	}*/
}
